package com.yeyks.controller.other;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.utils.AccountUtil;
import com.yeyks.activity.dal.domain.ActivityPrizeList;
import com.yeyks.activity.service.ActivityPrizeListService;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.ConfigKey;
import com.yeyks.common.dto.PublicBankDTO;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.activity.EditPublicBankParam;
import com.yeyks.common.utils.InvateCode;
import com.yeyks.commonReference.service.config.ConfigService;
import com.yeyks.commonReference.service.other.ActivityInfoService;
import com.yeyks.config.dal.dao.ConfigMapper;
import com.yeyks.config.dal.domain.Config;
import com.yeyks.other.model.AddActivityInfoParam;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "v3.8.1.update活动通知接口", description = "活动通知相关接口[添加,分页查询]")
@RestController
@RequestMapping("/ajax/activity")
public class ActivityController {


    @Autowired
    private ActivityInfoService activityInfoService;

    @Autowired
    private ActivityPrizeListService activityPrizeListService;

    @Autowired
    private ConfigMapper configMapper;

    @ApiOperation("分页查询活动信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "token", name = "token", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(value = "页码", name = "pageNum", paramType = "query", defaultValue = "1", dataType = "int", required = true),
            @ApiImplicitParam(value = "每页数量", name = "pageSize", paramType = "query", defaultValue = "10", dataType = "int", required = true)})
    @PostMapping("/list")
    public ResponseEntity<AjaxResponseBody> list(@RequestParam(name = "token", defaultValue = "", required = true) String token,
                                                 @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        return activityInfoService.list(token, pageNum, pageSize);
    }

    @ApiOperation("添加活动通知信息")
    @PostMapping("/add")
    public ResponseEntity<AjaxResponseBody> add(@Valid AddActivityInfoParam addActivityInfoParam) {

        return activityInfoService.add(addActivityInfoParam);
    }


    @ApiOperation("添加奖品清单")
    @PostMapping("/addPrizeList")
    public AjaxResponseBody addPrizeList(@RequestParam(name = "activityCode", defaultValue = "") String activityCode, @RequestParam(name = "prizeId", defaultValue = "") Integer prizeId, @RequestParam(name = "count", defaultValue = "10") Integer count) {

        List<ActivityPrizeList> activityPrizeLists = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            ActivityPrizeList apl = new ActivityPrizeList();
            apl.setActivityCode(activityCode);
            apl.setPrizeId(prizeId);
            apl.setRedeemCode(InvateCode.toSerialCode(i + prizeId));
            apl.setStatus(0);
            apl.setCreateTime(new Date());
            activityPrizeLists.add(apl);
        }
        return AjaxResponseBody.success(activityPrizeListService.insertByBatch(activityPrizeLists));
    }

    @NeedToken
    @ApiOperation("v3.8.1获取对公账户")
    @PostMapping("/public/bank/get")
    public AjaxResponseBody getPublicBank() {

        AjaxResponseBody body = AjaxResponseBody.failed();
        AccountUtils.getLoginUser();

        Config config = configMapper.selectValueByKey(ConfigKey.PUBLIC_BANK_ACCOUNT);
        if (null != config) {
            if (!StringUtils.isEmpty(config.getValue())) {
                PublicBankDTO dto = JSON.parseObject(config.getValue(), PublicBankDTO.class);
                body = AjaxResponseBody.success(dto);
            }
        }
        return body;
    }

    @NeedToken
    @ApiOperation("v3.8.1编辑对公账户")
    @PostMapping("/public/bank/edit")
    public AjaxResponseBody editPublicBank(@Valid EditPublicBankParam param) {

        AjaxResponseBody body = AjaxResponseBody.failed();

        AccountInfo accountInfo = AccountUtils.getLoginUser();
        if (accountInfo.getType() != AccountType.Administrator) {
            throw new ServiceException("没有权限操作！");
        }
        Config config = configMapper.selectValueByKey(ConfigKey.PUBLIC_BANK_ACCOUNT);
        if (!StringUtils.isEmpty(config.getValue())) {
            PublicBankDTO dto = JSON.parseObject(config.getValue(), PublicBankDTO.class);
            dto.setPublic_account_name(param.getPublic_account_name());
            dto.setPublic_account_card(param.getPublic_account_card());
            config.setValue(JSON.toJSONString(dto));
        }
        int i = configMapper.updateByPrimaryKey(config);
        if (i > 0) {
            body = AjaxResponseBody.success("更新对公账户成功");
        }
        return body;
    }
}
